package com.unisoc.floatkey;

import android.app.ActivityManager;
import android.app.KeyguardManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.ContentObserver;
import android.os.UserHandle;
import android.os.Handler;
import android.provider.Settings;
import android.os.SystemProperties;
import android.os.UserManager;
import android.view.View;
import android.util.Log;

public class SystemUIFloatKey {
    /* UNISOC: Modify for bug975748 {@ */
    public static final String ACTION_FLOATKEY_HIDE = "com.android.systemui.ACTION_FLOATKEY_HIDE";
    public static final String ACTION_FLOATKEY_STOP = "com.android.systemui.FLOATKEY_ACTION_STOP";
    public static final String ACTION_FLOATKEY_START = "com.android.systemui.FLOATKEY_ACTION_START";
    public static final String ACTION_FLOATKEY_RESTART = "com.android.systemui.FLOATKEY_ACTION_RESTART";
    /* @} */
    public static final String TAG = "SystemUIFloatKey";

    private static SystemUIFloatKey sInstance;
    private Context mContext;
    private FloatKeyView mFloatKeyView;
    private KeyguardManager mKeyguardManager = null;
    private UserManager mUserManager;

    /* UNISOC: Modify for bug990337 {@ */
    public static SystemUIFloatKey getInstance(Context context) {
        if (sInstance == null) {
            sInstance = new SystemUIFloatKey(context);
        }
        return sInstance;
    }
    /* @} */

    public SystemUIFloatKey(Context context){
        mContext = context;
        /* UNISOC: Modify for bug975748 {@ */
        mKeyguardManager = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
        /* @} */
        mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
    }

    public boolean isSupportFloatKey() {
        return true;
    }

    public void restart() {
        /* SPRD: Bug 882238 maybe NullPointerException @{ */
        if (mFloatKeyView != null) {
            /*UNISOC: Add for bug 1064405 @{*/
            mFloatKeyView.removeKeyPanelView();
            /*}@*/
            mFloatKeyView.removeFromWindow();
            /* UNISOC: Modify for bug978062 {@ */
            mFloatKeyView.setRestart(true);
            /* @} */
            mContext.getContentResolver().unregisterContentObserver(mScreenResolutionObserver); // UNISOC: Modify for bug 1190421
            // UNISOC: Modify for bug 1248691
            mContext.unregisterReceiver(mFloatKeyReceiver);
        }
        /* @} */
        start();
    }

    /* UNISOC: Modify for bug1190421 {@ */
    private static final String ACTION_SR_CHANGING = "sprd.action.super_resolution_state";
    private ContentObserver mScreenResolutionObserver = new ContentObserver(new Handler()) {
        @Override
        public void onChange(boolean selfChange) {
            Log.d(TAG, "screen resolution onChange selfChange=" + selfChange);
            updateResolution();
        }
    };

    private void updateResolution() {
        int srChanging = Settings.Global.getInt(mContext.getContentResolver(), ACTION_SR_CHANGING, 0);
        Log.d(TAG, "screen resolution updateResolution srChanging =" + srChanging);
        if (srChanging == 1) {
            if (mFloatKeyView != null) mFloatKeyView.setVisibility(View.INVISIBLE);
        } else {
            if (mFloatKeyView != null) mFloatKeyView.setVisibility(View.VISIBLE);
        }
    }
    /* @} */

    public void start(){
        Log.d(TAG, "start=");
        internal_start();
    }

    public void internal_start(){
        Log.d(TAG, "internal_start="+SystemProperties.getBoolean("ro.product.assistanttouch",true));
        // SPRD: Bug 598664  display only for owner
        if (SystemProperties.getBoolean("ro.product.assistanttouch",true)&& UserHandle.myUserId() == UserHandle.USER_OWNER) {
            //SPRD: add for assistant touch @{
            mFloatKeyView = new FloatKeyView(mContext);
            IntentFilter filter = new IntentFilter();
            filter.addAction(ACTION_FLOATKEY_STOP);
            filter.addAction(ACTION_FLOATKEY_START);
            filter.addAction(ACTION_FLOATKEY_RESTART);
            filter.addAction(Intent.ACTION_USER_SWITCHED);
            /* UNISOC: Modify for bug975748 {@ */
            filter.addAction(Intent.ACTION_USER_PRESENT);
            filter.addAction(ACTION_FLOATKEY_HIDE);
            /* @} */
            /* UNISOC: Modify for bug 1011943 {@ */
            filter.addAction(Intent.ACTION_USER_UNLOCKED);
            /* @} */
            mContext.registerReceiver(mFloatKeyReceiver, filter);
            if (Settings.Secure.getIntForUser(mContext.getContentResolver()
                                       , Settings.Secure.ASSISTANT_ON, 0, ActivityManager.getCurrentUser()) != 0) {
                mFloatKeyView.addToWindow();
                /* UNISOC: Modify for bug975748 {@ */
                checkKeyguardLockedState();
                /* @} */
                /* UNISOC: Modify for bug1190421 {@ */
                mContext.getContentResolver().registerContentObserver(Settings.Global.getUriFor(ACTION_SR_CHANGING),
                        false, mScreenResolutionObserver);
                /* @} */
            }
            // @}
        }
    }

    /* UNISOC: Modify for bug975748 {@ */
    public void checkKeyguardLockedState() {
        if (mKeyguardManager == null) {
            Log.d(TAG, "KeyguardManager is not init, can not get locke state");
            return;
        }
        if (!mUserManager.isUserUnlocked()){
            Log.d(TAG,"user is unlocked");
            setFloatViewVisible(false);
            return;
        }

        if (mKeyguardManager.isKeyguardLocked()) {
            Log.d(TAG, "Keyguard is locked ,please set float view inVisible");
            setFloatViewVisible(false);
        }
    }

    private void setFloatViewVisible(boolean isVisible) {
        int visibleState = isVisible ? View.VISIBLE : View.INVISIBLE;
        if (mFloatKeyView != null) {
            Log.d(TAG, "setFloatViewVisible "+isVisible);
            mFloatKeyView.setVisibility(visibleState);
        }
    }
    /* @} */

    /* UNISOC: Modify for bug990337 {@ */
    public boolean isFloatViewVisible() {
        boolean visible = false;
        if (mFloatKeyView != null) {
            visible = (mFloatKeyView.getVisibility() == View.VISIBLE ? true : false);
        }
        return visible;
    }
    /* @} */

    /**
     * SPRD: add for assistant touch
     */
    private final BroadcastReceiver mFloatKeyReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d(TAG, "receive intent:"+intent.getAction());
            if (mFloatKeyView == null)
                return;
            /* UNISOC: Modify for bug1190421 {@ */
            if (intent.getAction().equals(ACTION_FLOATKEY_STOP)) {
                mFloatKeyView.removeFromWindow();
                /*UNISOC: Add for bug 1232229 @{*/
                Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.ASSISTANT_ON, 0);
                /*}@*/
                mContext.getContentResolver().unregisterContentObserver(mScreenResolutionObserver);
            } else if (intent.getAction().equals(ACTION_FLOATKEY_START)) {
                /*UNISOC: Add for bug 1232229 @{*/
                Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.ASSISTANT_ON, 1);
                /*}@*/
                mFloatKeyView.addToWindow();
                mContext.getContentResolver().registerContentObserver(Settings.Global.getUriFor(ACTION_SR_CHANGING),
                        false, mScreenResolutionObserver);
            } else if (intent.getAction().equals(ACTION_FLOATKEY_RESTART)) {
                //UNISOC: Modify for bug 1372730
                setFloatViewVisible(true);
            /* UNISOC: Modify for bug975748 {@ */
            } else if (ACTION_FLOATKEY_HIDE.equals(intent.getAction())) {
                setFloatViewVisible(false);
            } else if ((Intent.ACTION_USER_PRESENT).equals(intent.getAction())) {
                if (mKeyguardManager != null && !mKeyguardManager.isKeyguardLocked() && mUserManager.isUserUnlocked()) {
                    setFloatViewVisible(true);
                }
            /* @} */
            /* UNISOC: Modify for bug 1011943 {@ */
            } else if ((Intent.ACTION_USER_UNLOCKED).equals(intent.getAction())){
                 if (mKeyguardManager != null && !mKeyguardManager.isKeyguardLocked()) {
                    setFloatViewVisible(true);
                 }

            /* @} */
            } else if (intent.getAction().equals((Intent.ACTION_USER_SWITCHED))) {
                if (Settings.Secure.getIntForUser(mContext.getContentResolver()
                                           , Settings.Secure.ASSISTANT_ON, 0, ActivityManager.getCurrentUser()) != 0) {
                    mFloatKeyView.addToWindow();
                    mContext.getContentResolver().registerContentObserver(Settings.Global.getUriFor(ACTION_SR_CHANGING),
                        false, mScreenResolutionObserver);
                } else {
                    mFloatKeyView.removeFromWindow();
                    mContext.getContentResolver().unregisterContentObserver(mScreenResolutionObserver);
                }
            }
            /* @} */
        }
    };
}
